package com.example.administrator.myapplication.register;

import android.app.Fragment;
import android.content.Intent;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Toast;

import com.example.administrator.myapplication.R;
import com.example.administrator.myapplication.login.LoginActivity;
import com.example.administrator.myapplication.tools.TestData;


import java.util.Timer;
import java.util.TimerTask;


import cn.bmob.v3.exception.BmobException;
import cn.bmob.v3.listener.SaveListener;


/**
 * Created by Administrator on 2017/11/7.
 */

public class RegisterFragment extends Fragment{

    private EditText register_username;
    private EditText register_password;
    private EditText register_sex;
    private EditText register_email;
    private EditText register_mobilePhoneNumber;
    private EditText register_userVerify;     //验证码
    private Button register_bt;


  
    


    @Override
    public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, Bundle savedInstanceState) {
        final View view=inflater.inflate(R.layout.fragment_register,container,false);

        register_username=(EditText) view.findViewById(R.id.register_user_et);
        register_password=(EditText) view.findViewById(R.id.register_pwd_et);
        register_sex=(EditText) view.findViewById(R.id.register_sex_et);
        register_email=(EditText) view.findViewById(R.id.register_email_et);
        register_mobilePhoneNumber=(EditText) view.findViewById(R.id.register_phone_et);
        register_userVerify=(EditText) view.findViewById(R.id.register_verify_et);
        register_bt=(Button)view.findViewById(R.id.register_bt);
        //创建数据库表
       final TestData bu = new TestData();
        register_bt.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                bu.setUsername(register_username.getText().toString());
                bu.setPassword(register_password.getText().toString());
                bu.setEmail(register_email.getText().toString());
                bu.setMobilePhoneNumber(register_mobilePhoneNumber.getText().toString());
                bu.setSex(register_sex.getText().toString());
                bu.setVerifynumber(register_userVerify.getText().toString());
                bu.signUp(new SaveListener<TestData>() {
                    public void done(TestData s, BmobException e) {
                            if(e==null){
                                Toast.makeText(getActivity(),"注册成功",Toast.LENGTH_SHORT).show();
                                //设置一个定时器 ，两秒之后执行以下代码
                                Timer timer =new Timer();
                                final Intent intent =new Intent(getActivity(), LoginActivity.class);
                                timer.schedule(new TimerTask() {
                                    @Override
                                    public void run() {
                                        startActivity(intent);   //两秒之后执行以下代码
                                    }
                                },2000);
                            }else{
                                Toast.makeText(getActivity(),"用户名已存在",Toast.LENGTH_SHORT).show();
                            }
                    }
                });
            }
        });
        return view;
    }
}
